package ute.mvc.controller.portal.admin;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ute.mvc.model.MyClass;
import ute.mvc.model.OpenCourse;
import ute.mvc.model.OpenCourse_Subject;
import ute.mvc.model.Subject;
import ute.mvc.model.User;
import ute.mvc.service.ClassService;
import ute.mvc.service.OpenCourseService;
import ute.mvc.service.SubjectService;
import ute.mvc.service.UserService;
import ute.mvc.util.JsonObject;


@Controller
@RequestMapping(value = {"/portal/opencourse"})
public class OpenCourseController {
	
	@Autowired
	OpenCourseService openCourseService;
	@Autowired
	ClassService classService;
	@Autowired
	UserService userService;
	@Autowired
	SubjectService subjectService;
	
	private static String openCourseId;
	
	@RequestMapping(value = "/{openCourseId}", method = RequestMethod.GET)
	public String getOpenCourse(@PathVariable String openCourseId, Model model) {
		this.openCourseId = openCourseId;	
		model.addAttribute("openCourse", openCourseService.getOpenCourseById(openCourseId));
		return "opencourse_detail";
	}
	
	@RequestMapping(value = "/edit/{openCourseId}", method = RequestMethod.GET)
	public String getEditOpenCourse(@PathVariable String openCourseId, Model model) {
		model.addAttribute("openCourse", openCourseService.getOpenCourseById(openCourseId));
		return "opencourse_edit";
	}
	
	@RequestMapping(value = "/edit/{openCourseId}", method = RequestMethod.POST)
	public String editOpenCourse(@PathVariable String openCourseId ,@Valid @ModelAttribute OpenCourse openCourse, BindingResult result, Model model) {
		if(result.hasErrors())
			return "opencourse_edit";
		
		openCourseService.updateOpenCourse(openCourse);
		return "redirect:../"+ openCourseId;
	}
	
	@RequestMapping(value = "/addclass/{openCourseId}", method = RequestMethod.GET)
	public String getNewClass(@PathVariable String openCourseId, Model model) {
		
		model.addAttribute("openCourseId", openCourseId);
		model.addAttribute("myClass", new MyClass());
		return "class_add";
	}
	
	@RequestMapping(value = "/addclass/{openCourseId}", method = RequestMethod.POST)
	public String NewClass(@PathVariable String openCourseId, @Valid @ModelAttribute MyClass myClass,
			BindingResult result, Model model) {
		
		if(result.hasErrors())
			return "class_add";
		
		if(classService.getClassById(myClass.getClassId())!= null) {
			result.rejectValue("classId", "classIdExist");
			return "class_add";
		}
		
		myClass.setOpenCourse(openCourseService.getOpenCourseById(openCourseId));
		classService.addClass(myClass);
		return "redirect:../"+ openCourseId;
	}
	
	@RequestMapping(value = "/addsubject/{openCourseId}")
	public String getAddSubject(@PathVariable String openCourseId, Model model) {
		this.openCourseId = openCourseId;
		model.addAttribute("openCourseId", openCourseId);
		return "add_subject";
	}
	
	@RequestMapping(value = "/addlecturer/{openCourseId}")
	public String getAddLecturer(@PathVariable String openCourseId, Model model) {
		this.openCourseId = openCourseId;
		model.addAttribute("openCourseId", openCourseId);
		return "add_lecturer";
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/listClassJSON")
	@ResponseBody
	public JsonObject getClassJSON() {
		OpenCourse openCourse = openCourseService.getOpenCourseById(openCourseId);
		List<MyClass> list = (List<MyClass>) openCourse.getListClass();
		
		System.out.println("Class " + list.size());
		
		JsonObject Json = new JsonObject();
		Json.setAaData(list);
		Json.setsEcho(1);
		Json.setiTotalDisplayRecords(list.size());
		Json.setiTotalRecords(list.size());
		return Json;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/listSubjectJSON")
	@ResponseBody
	public JsonObject getSubjectJSON() {
		
		List<Subject> list = openCourseService.getSubjectByOpenCourseId(openCourseId);
		
		System.out.println(openCourseId +  list.size());
		
		JsonObject Json = new JsonObject();
		Json.setAaData(list);
		Json.setsEcho(1);
		Json.setiTotalDisplayRecords(list.size());
		Json.setiTotalRecords(list.size());
		return Json;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/listLecturerJSON")
	@ResponseBody
	public JsonObject getTeacherJSON() {
		System.out.println("lay lectuer");
		List<User> list = openCourseService.getLecturerByOpenCourseId(openCourseId);
		System.out.println(list.size());
		
		JsonObject Json = new JsonObject();
		Json.setAaData(list);
		Json.setsEcho(1);
		Json.setiTotalDisplayRecords(list.size());
		Json.setiTotalRecords(list.size());
		return Json;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/addsubject/listAddSubjectJSON")
	@ResponseBody
	public JsonObject getAddSubject() {
		System.out.println("lay subject" + openCourseId);
		List<Subject> list = openCourseService.getListAddSubjectByOpenCourseId(openCourseId);
		System.out.println(list.size());
		
		JsonObject Json = new JsonObject();
		Json.setAaData(list);
		Json.setsEcho(1);
		Json.setiTotalDisplayRecords(list.size());
		Json.setiTotalRecords(list.size());
		System.out.println(list.size() + " " + Json.toString());
		return Json;
	}
	@ResponseBody
	@RequestMapping("/addsubject/addSubject")
	public String addStudentToClass(@RequestBody String subjects) {
		try {
			OpenCourse openCourse = openCourseService.getOpenCourseById(openCourseId);
			Subject subject;
			OpenCourse_Subject openCourse_Subject;
			
			String[] subjectIds = subjects.split(";");
			for(String subjectId : subjectIds){
				openCourseService.addSubjectToOpenCourse(openCourseId, subjectId);
			}
			
			return "sucess";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "fail";
		}
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/addlecturer/listAddLecturerJSON")
	@ResponseBody
	public JsonObject getAddLecturer() {
		System.out.println("lay lecturer" + openCourseId);
		List<User> list = openCourseService.getListAddLecturerByOpenCourseId(openCourseId);
		System.out.println(list.size());
		JsonObject Json = new JsonObject();
		Json.setAaData(list);
		Json.setsEcho(1);
		Json.setiTotalDisplayRecords(list.size());
		Json.setiTotalRecords(list.size());
		System.out.println(list.size() + " " + Json.toString());
		return Json;
	}
	
	@ResponseBody
	@RequestMapping(value = "/addlecturer/addLecturer")
	public String addLecturerToOpenCourse(@RequestBody String lecturers) {
		String[] lecturerIds = lecturers.split(";");
		
		try {
			openCourseService.addLecturerToOpenCourse(openCourseId, lecturerIds);
			
			return "success";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "fail";
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public String deleteOpenCourse(@RequestBody String deletes) {
		String[] classIds, subjectIds, lecturerEmails;
		String classes, subjects, lecturers;
		classes = deletes.substring(deletes.indexOf("Class") + 5, deletes.indexOf("Subject"));
		subjects = deletes.substring(deletes.indexOf("Subject") + 7, deletes.indexOf("Lecturer"));
		lecturers = deletes.substring(deletes.indexOf("Lecturer") + 8, deletes.length());

		classIds = classes.split(";");
		subjectIds = subjects.split(";");
		lecturerEmails = lecturers.split(";");
		
		if(openCourseService.removeLecturer(lecturerEmails, openCourseId) 
				&& openCourseService.removeSubject(subjectIds, openCourseId) 
				&& openCourseService.deleteClass(classIds)){
			return "success";
		} else {
			return "fail";
		}
	}
}
